package com.wu.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 格式转换
 */
public class CastUtil {

    public static String castFormat(String str) {
        String substring = str.substring(1, str.length()-1);
        String[] strings = substring.split(",");

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
            stringBuilder.append(strings[i].substring(9,strings[i].length()-1));
            if (i < strings.length - 1) {
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");

        return stringBuilder.toString();
    }

    //Object => List<T>
    public static <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }

}
